package com.gxa.interceptor;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxa.utils.JWTUtils;
import com.gxa.utils.ResponseData;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: 42818
 * @date: 2022/8/31 17:22
 * @Software: IntelliJ IDEA
 * @Package_Name: com.gxa.interceptor
 * @ClassName: LoginInterceptor
 * @Description:
 **/
//public class LoginInterceptor implements HandlerInterceptor {
//    @Override
//    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
//        //前置处理
//
//        //获取前端的传过来的token
//        String token = httpServletRequest.getHeader("token");
//
//        if(token == null || token.equals("") || token.equals("null")){
//            //用户没有登录
//            httpServletResponse.setCharacterEncoding("UTF-8");
//            httpServletResponse.setContentType("application/json;charset=utf-8");
//
//            //封装一个ResponseData转换的json格式对象
//            ObjectMapper objectMapper = new ObjectMapper();
//            //把一个对象转换为json格式字符串
//            String jsonResult = objectMapper.writeValueAsString(ResponseData.error(101,"没有登录"));
//
//            httpServletResponse.getWriter().write(jsonResult);
//            return false;
//
//        }else{
//
//            //验证token的合法性
//            boolean result =  JWTUtils.verify(token);
//            if(!result){
//                //用户每天通过验证
//                httpServletResponse.setCharacterEncoding("UTF-8");
//                httpServletResponse.setContentType("application/json;charset=utf-8");
//
//                //封装一个ResponseData转换的json格式对象
//                ObjectMapper objectMapper = new ObjectMapper();
//                //把一个对象转换为json格式字符串
//                String jsonResult = objectMapper.writeValueAsString(ResponseData.error(102,"验证失败，请重新登录"));
//
//                httpServletResponse.getWriter().write(jsonResult);
//                return false;
//            }
//
//        }
//
//        return true;
//    }
//
//    @Override
//    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
//
//    }
//
//    @Override
//    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
//
//    }
//}
